﻿#Region "Copyright"

' Copyright 2007 Ben Reichelt - http://benreichelt.net
' 
' Licensed under the Apache License, Version 2.0 (the "License");
' you may not use this file except in compliance with the License.
' You may obtain a copy of the License at
' 
'     http://www.apache.org/licenses/LICENSE-2.0
' 
' Unless required by applicable law or agreed to in writing, software
' distributed under the License is distributed on an "AS IS" BASIS,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' See the License for the specific language governing permissions and
' limitations under the License.


#End Region

Public Class MetaDataAssociation

    Private _metaData As XmlMetaData
    Private _node As XElement

    Public Sub New(ByVal node As XElement, ByVal metaData As XmlMetaData)
        _node = node
        _metaData = metaData
    End Sub

    ''' <summary>
    ''' The name of this association
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property MemberName() As String
        Get
            If _node.Attribute("Member") IsNot Nothing Then
                Return _node.Attribute("Member").Value
            End If
            Return String.Empty
        End Get
    End Property

    ''' <summary>
    ''' The foreign key of this Association
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property OtherKey() As String
        Get
            If _node.Attribute("OtherKey") IsNot Nothing Then
                Return _node.Attribute("OtherKey").Value
            End If
            Return String.Empty
        End Get
    End Property

    ''' <summary>
    ''' The "domestic" key of this Association
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property ThisKey() As String
        Get
            If _node.Attribute("ThisKey") IsNot Nothing Then
                Return _node.Attribute("ThisKey").Value
            End If
            Return String.Empty
        End Get
    End Property

    ''' <summary>
    ''' Renames this Association to the new name 
    ''' </summary>
    ''' <param name="newName">The new name for this association</param>
    ''' <remarks></remarks>
    Public Sub RenameTo(ByVal newName As String)

        Dim node As XElement = _node
        If node.Attribute("Member") IsNot Nothing Then
            node.SetAttributeValue("Member", newName)
        Else
            Dim member As XAttribute = New XAttribute("Member", newName)
            node.Add(member)
        End If

    End Sub

    ''' <summary>
    ''' Changes the name of the key from this table that is used in the Association.
    ''' This is used when a column is renamed, all the references need to be updated.
    ''' </summary>
    ''' <param name="newName">The new column name for the key</param>
    ''' <remarks></remarks>
    Public Sub ChangeThisKey(ByVal newName As String)
        _node.SetAttributeValue(Names.ThisKeyXName, newName)
    End Sub

    ''' <summary>
    ''' Changes the name of the key from the foreign table that is used in the Association.
    ''' This is used when a column is renamed, all the references need to be updated.
    ''' </summary>
    ''' <param name="newName">The new column name for the key</param>
    ''' <remarks></remarks>
    Public Sub ChangeOtherKey(ByVal newName As String)
        _node.SetAttributeValue(Names.OtherKeyXName, newName)
    End Sub

End Class
